package com.example.docking.controller;

import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jingxin.request.MaterialDao;
import com.example.docking.service.impl.DataServiceImpl;
import com.example.docking.service.impl.MaterialServiceImpl;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 保存物料信息
 *
 * @ClassName MaterialController
 * @Description 需求：敬信推送物料信息到金蝶
 * 字段：物料名称
 * ：单位
 * 逻辑：先判断单位 如果金蝶有 查询返回id
 * 如果没有 新增返回id
 * 然后存入商品信息
 * @Author zhanghuiping
 * @Date 2025 - 04 - 07 03:40:33
 */


@RestController
@RequestMapping("/material")
public class MaterialController {
    private static final Logger logger = LoggerFactory.getLogger(MaterialController.class);

    @PostMapping("/save")
    public String Insertmaterial(@Valid @RequestBody MaterialDao material) {
        logger.info("敬信物料请求进来了insert material");
        DataServiceImpl Service = new DataServiceImpl();
        MaterialServiceImpl materialService = new MaterialServiceImpl();
        ResponseData responseData = new ResponseData();
        Gson gson = new Gson();
        try {
            //数据新增到ERP
            ResponseData responseData1 = materialService.insertMaterial(Service, material);
            responseData.setErrcode(responseData1.getErrcode());
            responseData.setDescription(responseData1.getDescription());
            responseData.setData(responseData1.getData());
            //数据新增到CRM
            //数据新增到SRM
        } catch (Exception e) {
            logger.error("保存物料信息时发生异常", e);
            throw new RuntimeException(e);
        }
        logger.info("敬信返回响应" + responseData);
        return gson.toJson(responseData);
    }
}